package com.qfedu.yanglao20admin.visit.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20admin.visit.common.SatisfactionEnum;
import com.qfedu.yanglao20admin.visit.common.VisitStateEnum;
import com.qfedu.yanglao20admin.visit.dao.ReturnVisitDao;
import com.qfedu.yanglao20admin.visit.param.ReturnVisitParam;
import com.qfedu.yanglao20admin.visit.param.VisitFlagParam;
import com.qfedu.yanglao20admin.visit.service.ReturnVisitService;
import com.qfedu.yanglao20admin.visit.vo.RecordVO;
import com.qfedu.yanglao20admin.visit.vo.ReturnVisitVO;
import com.qfedu.yanglao20admin.visit.vo.VisitVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @author yujie
 * @data 2023/5/4 20:13
 */
@Service
public class ReturnVisitServiceImpl implements ReturnVisitService {

    @Resource
    private ReturnVisitDao returnVisitDao;

    @Override
    public Page<ReturnVisitVO> pageReturnVisit(ReturnVisitParam param) {
        // 计算偏移量
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<ReturnVisitVO> page = new Page<>(param.getPageNum(), param.getPageSize());

        List<ReturnVisitVO> returnVisitVOS = returnVisitDao.selectList(param);

        //  循环遍历
        // 使用枚举展示状态具体信息
        for (ReturnVisitVO vo :
                returnVisitVOS) {
            VisitStateEnum stateById = VisitStateEnum.getStateById(vo.getVisitFlag());
            vo.setVisitFlag(stateById.getId());
        }

        // 页码
        page.setCurrent(param.getPageNum());
        // 每页展示记录数
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = returnVisitDao.selectCount(null);
        page.setTotal(aLong);

        page.setRecords(returnVisitVOS);

        return page;
    }

    /**
     * 添加回访信息
     *
     * @param param
     */
    @Override
    public void updateVisit(VisitFlagParam param) {
        returnVisitDao.updateVisit(param);
    }

    /**
     * 根据 id 查询回访记录信息
     *
     * @param id
     * @return
     */
    @Override
    public RecordVO selectRecordById(Integer id) {

        RecordVO recordVO = returnVisitDao.selectRecordById(id);

        SatisfactionEnum stateById = SatisfactionEnum.getStateById(recordVO.getSatisfaction());
        String desc = stateById.getDesc();

        recordVO.setSatisfactionDesc(desc);

        return recordVO;
    }

    @Override
    public VisitVO selectCount() {

        VisitVO visitVO = new VisitVO();

        // 计算得到 订单数量
        int sum = returnVisitDao.selectCount();
        // 回访数量
        int num = returnVisitDao.selectCountByVisitFlag();
        // 满意度
        BigDecimal bigDecimal2 = returnVisitDao.selectAvg();

        visitVO.setOrderNum(sum);
        visitVO.setVisitNum(num);

        float b1 = (float) num;
        float b2 = (float) sum;

        // 将回访率转化为百分号信形式
        DecimalFormat decFormat = new DecimalFormat("#%");
        String format = decFormat.format(b1 / b2);
        visitVO.setVisitRate(format);

        BigDecimal decimal = new BigDecimal(String.valueOf(bigDecimal2));
        visitVO.setAverage(decimal.setScale(2, BigDecimal.ROUND_HALF_UP));

        return visitVO;
    }
}
